package com.xebest.web.service.impl;


import com.xebest.common.util.XeBaseUtil;
import com.xebest.util.seq.FinanceSeqUtil;
import com.xebest.web.form.XeVenFinSalesBillForm;
import com.xebest.web.mapper.read.XeVenFinSalesBillReadMapper;
import com.xebest.web.mapper.write.XeVenFinSalesBillDetailWriteMapper;
import com.xebest.web.mapper.write.XeVenFinSalesBillWriteMapper;
import com.xebest.web.model.XeVenFinSalesBillDetailEntity;
import com.xebest.web.model.XeVenFinSalesBillEntity;
import com.xebest.web.service.XeVenFinSalesBillService;
import com.xebest.web.view.XeVenFinSalesBillView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @desc:收款单业务操作实现接口
 * @author: wangyp
 * @createTime: 2017/8/2 19:38
 * @history:
 * @version:v1.0
 */
@Service
public class XeVenFinSalesBillSrrviceImpl implements XeVenFinSalesBillService {


    private static final Logger log = LoggerFactory.getLogger(XeVenFinSalesBillSrrviceImpl.class);//日志记录类


    @Autowired
    private XeVenFinSalesBillReadMapper xeVenFinSalesBillReadMapper;
    @Autowired
    private XeVenFinSalesBillWriteMapper xeVenFinSalesBillWriteMapper;
    @Autowired
    private XeVenFinSalesBillDetailWriteMapper xeVenFinSalesBillDetailWriteMapper;

    /**
     * 收款单列表
     *
     * @param form
     * @return int
     * @author: wangyp
     * @createTime: 2017/8/2 19:34
     * @history:
     */
    public List<XeVenFinSalesBillView> queryXeVenFinSalesBillList(XeVenFinSalesBillForm form) {
        List<XeVenFinSalesBillView> finSalesBillList = xeVenFinSalesBillReadMapper.queryXeVenFinSalesBillList(form);
        for (XeVenFinSalesBillView view : finSalesBillList) {
            if (view.getCaAreaId()!=null) {
                String area = XeBaseUtil.baseUtil.getOrgAreaName(Integer.parseInt(view.getCaAreaId()));
                view.setCaAreaId(area);
            }
        }
        return finSalesBillList;
    }

    /**
     * 修改付款单信息
     *
     * @Author:wangyp
     * @Description:查询收款单列表
     * @Date:下午 8:28 2017/08/12 0019
     * @params: * @param XeVenFinSalesBillForm
     * @return: List<XeVenFinSalesBillView>
     */
    public int updateXeVenFinSalesBill(XeVenFinSalesBillForm form) {
        return xeVenFinSalesBillWriteMapper.updateXeVenFinSalesBill(form);
    }

    /**
     * 插入付款单
     *
     * @param :
     * @author: Wangyp
     * @createTime: 2017/8/4 15:58
     * @return:
     */
    public int insertFinSalesBill(XeVenFinSalesBillEntity entity) throws Exception {
        return xeVenFinSalesBillWriteMapper.insertFinSalesBill(entity);
    }

    /**
     * 插入收款单
     * @param :
     * @author: Wangyp
     * @createTime: 2017/8/14 18:01
     * @return:
     */
    @Override
    @Transactional(value = "writeTransactionManager")
    public int saveFinSalesBill(XeVenFinSalesBillEntity form) throws Exception {
        int result = 0;
        String sbillCode = "";
        if (StringUtils.isEmpty(form.getSbillCode())) {
            sbillCode = FinanceSeqUtil.seqUtils.createSbillCode();
            log.info("生成的收款单单号sbillCode：{}", sbillCode);
            form.setSbillCode(sbillCode);
        }
        //添加收款单
        result = xeVenFinSalesBillWriteMapper.insertFinSalesBill(form);
        //添加收款单商品明细
        List<XeVenFinSalesBillDetailEntity> billEntity = form.getBillDetailEntityList();
        if (result > 0) {
            if (billEntity != null && billEntity.size() > 0) {
                for (XeVenFinSalesBillDetailEntity entity:billEntity){
                    entity.setSbillCode(sbillCode);
                }
                xeVenFinSalesBillDetailWriteMapper.batchInsertSalesBillDetail(billEntity);
            }
        }
        return result;
    }
}
